package cool.taomu.toolkit.cacheable.module

import com.google.inject.AbstractModule
import com.google.inject.name.Names
import cool.taomu.toolkit.TaomuConfigure
import cool.taomu.toolkit.cacheable.ICacheService
import cool.taomu.toolkit.cacheable.RedisCacheService
import cool.taomu.toolkit.cacheable.provider.RedisProvider
import java.util.Map
import redis.clients.jedis.JedisPool

class RedisModule extends AbstractModule {

    TaomuConfigure config;

    new(TaomuConfigure config) {
        this.config = config;
    }

    override configure() {
        config.binder("taomu.cacheable", [
            if (it instanceof Map) {
                if (it.containsKey("type") && (it.get("type") as String).equalsIgnoreCase("redis")) {
                    var key = #["taomu.cacheable.name", it.get("name")].join(".")
                    bind(JedisPool).toProvider(new RedisProvider(it))
                    bind(ICacheService).annotatedWith(Names.named(key)).to(RedisCacheService)
                }
            }
        ])
    }
}
